package game.ui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

public class RegisterFrame extends JFrame implements MouseListener {
    JTextField jTextFieldName = new JTextField();
    JTextField jTextFieldPassword1 = new JTextField();
    JTextField jTextFieldPassword2 = new JTextField();
    //注册按钮
    JButton jButtonRegister = new JButton();
    //重置按钮
    JButton jButtonReset = new JButton();
    ArrayList<User> userList = UserList.userList();
    //注册相关
    public RegisterFrame() {
        initFrame();
        initView();
        this.setVisible(true);
    }

    public void initFrame() {
        this.setSize(488, 500);
        //设置界面标题
        this.setTitle("拼图游戏注册");
        //设置置顶
        this.setAlwaysOnTop(true);
        //设置界面在屏幕居中
        this.setLocationRelativeTo(null);
        //按下关闭时结束程序
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public void initView() {
        JLabel jLabelUserName = new JLabel(new ImageIcon("JigsawPuzzles\\image\\register\\注册用户名.png"));
        jLabelUserName.setBounds(80, 138, 80, 19);
        this.getContentPane().add(jLabelUserName);

        JLabel jLabelPassword1 = new JLabel(new ImageIcon("JigsawPuzzles\\image\\register\\注册密码.png"));
        jLabelPassword1.setBounds(90, 200, 70, 18);
        this.getContentPane().add(jLabelPassword1);

        JLabel jLabelPassword2 = new JLabel(new ImageIcon("JigsawPuzzles\\image\\register\\再次输入密码.png"));
        jLabelPassword2.setBounds(80, 260, 100, 18);
        this.getContentPane().add(jLabelPassword2);

        jTextFieldName.setBounds(180, 134, 200, 30);
        this.getContentPane().add(jTextFieldName);

        jTextFieldPassword1.setBounds(180, 195, 200, 30);
        this.getContentPane().add(jTextFieldPassword1);

        jTextFieldPassword2.setBounds(180, 256, 200, 30);
        this.getContentPane().add(jTextFieldPassword2);

        //注册按钮
        jButtonRegister.setIcon(new ImageIcon("JigsawPuzzles\\image\\register\\注册按钮.png"));
        jButtonRegister.setBounds(123, 310, 128, 47);
        jButtonRegister.setBorderPainted(false);
        jButtonRegister.setContentAreaFilled(false);
        jButtonRegister.setVisible(true);
        this.getContentPane().add(jButtonRegister);

        //重置按钮
        jButtonReset.setIcon(new ImageIcon("JigsawPuzzles\\image\\register\\重置按钮.png"));
        jButtonReset.setBounds(256, 311, 128, 47);
        jButtonReset.setBorderPainted(false);
        jButtonReset.setContentAreaFilled(false);
        jButtonReset.setVisible(true);
        this.getContentPane().add(jButtonReset);

        //事件绑定
        jButtonRegister.addMouseListener(this);
        jButtonReset.addMouseListener(this);

        //背景图
        JLabel background = new JLabel(new ImageIcon("JigsawPuzzles\\image\\register\\background.png"));
        background.setBounds(0, 0, 470, 390);
        this.getContentPane().add(background);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        Object obj = e.getSource();
        String name = jTextFieldName.getText();
        String password = jTextFieldPassword1.getText();
        String password2 = jTextFieldPassword2.getText();
        if (obj == jButtonRegister) {

            User user = new User(name, password);
            if (name.length()==0 || password.length()==0 || password2.length()==0) {
                DialogUtil.showDialog("请填写完整信息后再注册！");
            } else if (!userInfo(user)) {
                DialogUtil.showDialog("用户名已存在!");
            } else if (!password.equals(password2)) {
                DialogUtil.showDialog("两次密码不一致！");
            }else {
                System.out.println("通过验证，注册成功！");
                DialogUtil.showDialog("注册成功！");
                //userList.add(user);
                UserList.addUserList(user);
                this.setVisible(false);
                new LoginFrame();
            }
        }
        if (obj == jButtonReset) {
            jTextFieldName.setText("");
            jTextFieldPassword1.setText("");
            jTextFieldPassword2.setText("");
        }

    }

    @Override
    public void mousePressed(MouseEvent e) {
        Object obj = e.getSource();
        if (obj ==  jButtonRegister) {
            jButtonRegister.setIcon(new ImageIcon("JigsawPuzzles\\image\\register\\注册按下.png"));
        } else if (obj == jButtonReset) {
            jButtonReset.setIcon(new ImageIcon("JigsawPuzzles\\image\\register\\重置按下.png"));
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        Object obj = e.getSource();
        if (obj ==  jButtonRegister) {
            jButtonRegister.setIcon(new ImageIcon("JigsawPuzzles\\image\\register\\注册按钮.png"));

        } else if (obj == jButtonReset) {
            jButtonReset.setIcon(new ImageIcon("JigsawPuzzles\\image\\register\\重置按钮.png"));
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }


    @Override
    public void mouseExited(MouseEvent e) {

    }
    //判断是否存在用户
    public boolean userInfo(User user) {

        if (userList.size() == 0) {
            System.out.println("暂无信息");
            return true;
        }
        for (int i = 0; i < userList.size(); i++) {
            User right = userList.get(i);
            if (right.getUsername().equals(user.getUsername())) {
                return true;
            }
        }
        return false;

    }
}
